﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;

#if WINDOWS_PHONE
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
#else
#endif

namespace TestUi
{
    public class TestHelper
    {
#if WINDOWS_PHONE
        private NavigationService _navigationService;

        public Pivot CreateTestPivot(NavigationService navigationService)
        {
            _navigationService = navigationService;
            Pivot testContainer = new Pivot();
            Dictionary<string, ListBox> featureLists = new Dictionary<string, ListBox>();
#else
        public TabControl CreateTestTab()
        {
            TabControl testContainer = new TabControl();
            Dictionary<string, TabItem> featureLists = new Dictionary<string, TabItem>();
#endif     
            Assembly assembly = Assembly.GetExecutingAssembly();
#if WINDOWS_PHONE
#else
            TabControl tabControl = new TabControl();
#endif
            foreach (var type in assembly.GetTypes())
            {
                if (type.Namespace.Contains(".Tests"))
                {
                    int idx = type.Namespace.IndexOf(".Test") + 7;
                    if (idx >= type.Namespace.Length)
                    {
                        continue;
                    }
                    var featureArea = type.Namespace.Substring(idx);
#if WINDOWS_PHONE
                    ListBox lb = null;
                    if (featureLists.ContainsKey(featureArea))
                    {
                        lb = featureLists[featureArea];
                    }
                    else
                    {
                        lb = new ListBox();
                        lb.SelectionChanged += new SelectionChangedEventHandler(lb_SelectionChanged);
                        featureLists.Add(featureArea, lb);
                    }

                    if (type.Name.StartsWith("<>"))
                    {
                        continue;
                    }
                    TextBlock tb = new TextBlock();
                    tb.Tag = type;
                    tb.Text = type.Name;
                    tb.FontSize = 40;
                    tb.Margin = new System.Windows.Thickness(10);
                    lb.Items.Add(tb);              
#else
                    if (type.Name.StartsWith("<>"))
                    {
                        continue;
                    }

                    TabItem tabItem;
                    TabControl childTabControl ;

                    // Create Main Item
                    if (featureLists.ContainsKey(featureArea))
                    {
                        tabItem = featureLists[featureArea];
                        childTabControl = tabItem.Content as TabControl;
                    }
                    else
                    {
                        tabItem = new TabItem();
                        tabItem.Header = featureArea;
                        //tc.SelectionChanged += new SelectionChangedEventHandler(lb_SelectionChanged);
                        featureLists.Add(featureArea, tabItem);
                        childTabControl = new TabControl();
                        tabItem.Content = childTabControl;
                        tabControl.Items.Add(tabItem);
                    }

                    // Create Child TabItem
                    TypeDelegator td = new TypeDelegator(type);
                    var guiElement = td.GetConstructors()[0].Invoke(null) as FrameworkElement;

                    TabItem childItem = new TabItem();
                    childItem.Header = type.Name;
                    childItem.Content = guiElement;

                    childTabControl.Items.Add(childItem);
#endif
                }
            }
#if WINDOWS_PHONE
            var sortedFeatureLists = from i in featureLists
                                     orderby i.Key
                                     select i;

            foreach (var list in sortedFeatureLists)
            {
                PivotItem pi = new PivotItem();
                pi.Header = list.Key;
                pi.Content = list.Value;
                testContainer.Items.Add(pi);
            }
            return testContainer ;
#else
            return tabControl;
#endif
        }

#if WINDOWS_PHONE
        void lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            ListBox lb = sender as ListBox;
            var si = lb.SelectedItem;
            TextBlock tb = si as TextBlock;
            Type type = tb.Tag as Type;

            string page = type.FullName.Replace('.', '/').Replace("TestUi", "") + ".xaml";
            Uri uri = new Uri(page, UriKind.Relative);
            _navigationService.Navigate(uri);
        }
#endif
    }
}
